<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="钞箱编号" prop="cassetteId">
        <el-input v-model="queryParams.cassetteId" placeholder="请输入钞箱编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="ATM钞箱机构id" prop="orgId" label-width="200px">
        <el-select v-model="queryParams.orgId" placeholder="请选择ATM钞箱机构" clearable>
          <el-option v-for="item in atmCassetteOrgList" :key="item.id" :label="item.orgName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="ATM品牌ID" prop="cassetteBrandId" label-width="100px">
        <el-select v-model="queryParams.cassetteBrandId" placeholder="请选择ATM品牌" clearable>
          <el-option v-for="item in atmCassetteBrandList" :key="item.id" :label="item.brandName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="钞箱类型" prop="cassetteType">
        <el-input v-model="queryParams.cassetteType" placeholder="请输入钞箱类型" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="钞箱状态" prop="cassetteStatus">
        <el-select v-model="queryParams.cassetteStatus" placeholder="请选择钞箱状态" clearable>
          <el-option v-for="dict in cassette_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="条码状态" prop="barcodeStatus">
        <el-select v-model="queryParams.barcodeStatus" placeholder="请选择条码状态" clearable>
          <el-option v-for="dict in barcode_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="在库状态" prop="inoutStatus">
        <el-select v-model="queryParams.inoutStatus" placeholder="请选择在库状态" clearable>
          <el-option v-for="dict in inout_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="入出库调度者" prop="inoutBy" label-width="100px">
        <el-input v-model="queryParams.inoutBy" placeholder="请输入入出库调度者" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['service:atmCassette:add']">新建</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="CirclePlus" @click="handleAdd"
          v-hasPermi="['service:atmCassette:add']">批量新建</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="EditPen" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:atmCassette:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:atmCassette:edit']">批量修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['service:atmCassette:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Printer" :disabled="multiple" @click="handleAdd"
          v-hasPermi="['service:atmCassette:remove']">打印条码</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="WindPower" :disabled="multiple" @click="handleAdd"
          v-hasPermi="['service:atmCassette:remove']">修改条码状态为已打印</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="EditPen" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:atmCassette:edit']">冻结款箱条码</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="EditPen" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:atmCassette:edit']">解除冻结</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="EditPen" :disabled="single" @click="handleUpdate"
          v-hasPermi="['service:atmCassette:edit']">修改条码(含二维码)状态为已打印</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['service:atmCassette:export']">导出各银行钞箱总数汇总表</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="atmCassetteList" @selection-change="handleSelectionChange">
      <el-table-column label="" align="center" type="index" width="50" />
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="钞箱编号" align="center" prop="cassetteId" />
      <el-table-column label="钞箱状态" align="center" prop="cassetteStatus">
        <template #default="scope">
          <dict-tag :options="cassette_status" :value="scope.row.cassetteStatus" />
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="cassetteBrandId">
        <template #default="scope">
          {{ getAtmCassetteBrandName(scope.row.cassetteBrandId) }}
        </template>
      </el-table-column>

      <el-table-column label="钞箱类型" align="center" prop="cassetteType" />

      <el-table-column label="所属机构" align="center">
        <template #default="scope">
          {{ getAtmCassetteOrgName(scope.row.orgId) }}
        </template>
      </el-table-column>

      <el-table-column label="条码状态" align="center" prop="barcodeStatus">
        <template #default="scope">
          <dict-tag :options="barcode_status" :value="scope.row.barcodeStatus" />
        </template>
      </el-table-column>
      <el-table-column label="打印时间" align="center" prop="printTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.printTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="钞箱状态变更时间" align="center" prop="cassetteStatusChangeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.cassetteStatusChangeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入出库时间" align="center" prop="inoutTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inoutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="在库状态" align="center" prop="inoutStatus">
        <template #default="scope">
          <dict-tag :options="inout_status" :value="scope.row.inoutStatus" />
        </template>
      </el-table-column>
      <el-table-column label="入出库调度者" align="center" prop="inoutBy" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['service:atmCassette:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['service:atmCassette:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改ATM钞箱管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="atmCassetteRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="ATM钞箱机构id" prop="orgId" label-width="150px">
          <el-select v-model="form.orgId" placeholder="请选择ATM钞箱机构">
            <el-option v-for="item in atmCassetteOrgList" :key="item.id" :label="item.orgName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="ATM品牌ID" prop="cassetteBrandId" label-width="150px">
          <el-select v-model="form.cassetteBrandId" placeholder="请选择ATM品牌">
            <el-option v-for="item in atmCassetteBrandList" :key="item.id" :label="item.brandName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="钞箱类型" prop="cassetteType" label-width="150px">
          <el-input v-model="form.cassetteType" placeholder="请输入钞箱类型" />
        </el-form-item>
        <el-form-item label="备注" prop="remark" label-width="150px">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AtmCassette">

import { listAtmCassetteBrand} from "@/api/service/atmCassetteBrand"
import { listAtmCassetteOrg} from "@/api/service/atmCassetteOrg"
import { listAtmCassette, getAtmCassette, delAtmCassette, addAtmCassette, updateAtmCassette } from "@/api/service/atmCassette"


const { proxy } = getCurrentInstance()
const { inout_status, barcode_status, cassette_status, device_brand } = proxy.useDict('inout_status', 'barcode_status', 'cassette_status', 'device_brand')

const atmCassetteList = ref([])
const atmCassetteBrandList = ref([])
const atmCassetteOrgList = ref([])

const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    cassetteId: null,
    orgId: null,
    cassetteBrandId: null,
    cassetteType: null,
    cassetteStatus: null,
    barcodeStatus: null,
    inoutStatus: null,
    inoutBy: null,
  },
  rules: {
    cassetteId: [
      { required: true, message: "钞箱编号不能为空", trigger: "blur" }
    ],
    orgId: [
      { required: true, message: "ATM钞箱机构ID不能为空", trigger: "blur" }
    ],
    cassetteBrandId: [
      { required: true, message: "ATM品牌ID不能为空", trigger: "blur" }
    ],
    cassetteType: [
      { required: true, message: "钞箱类型不能为空", trigger: "blur" }
    ],
    cassetteStatus: [
      { required: true, message: "钞箱状态不能为空", trigger: "change" }
    ],
    barcodeStatus: [
      { required: true, message: "条码状态不能为空", trigger: "change" }
    ],
    inoutStatus: [
      { required: true, message: "在库状态不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询ATM钞箱管理列表 */
function getList() {
  loading.value = true

  listAtmCassetteBrand({}, false).then(response => {
    atmCassetteBrandList.value = response.rows
  })

  listAtmCassetteOrg({}, false).then(response => {
    atmCassetteOrgList.value = response.rows
  })

  listAtmCassette(queryParams.value).then(response => {
    atmCassetteList.value = response.rows
    total.value = response.total
    loading.value = false
  })

}

// 根据ID获取品牌名称
function getAtmCassetteBrandName(brandId) {
  if (!brandId) {
    return ''
  }
  const brand = atmCassetteBrandList.value.find(brand => brand.id === brandId)
  return brand ? brand.brandName : brandId
}

// 根据ID获取机构名称
function getAtmCassetteOrgName(orgId) {
  if (!orgId) {
    return ''
  }
  const org = atmCassetteOrgList.value.find(org => org.id === orgId)
  return org ? org.orgName : orgId
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    cassetteId: null,
    orgId: null,
    cassetteBrandId: null,
    cassetteType: null,
    cassetteStatus: null,
    barcodeStatus: null,
    printTime: null,
    cassetteStatusChangeTime: null,
    inoutTime: null,
    inoutStatus: 0,
    inoutBy: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("atmCassetteRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加ATM钞箱管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getAtmCassette(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改ATM钞箱管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["atmCassetteRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateAtmCassette(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAtmCassette(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除ATM钞箱管理编号为"' + _ids + '"的数据项？').then(function () {
    return delAtmCassette(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('service/atmCassette/export', {
    ...queryParams.value
  }, `atmCassette_${new Date().getTime()}.xlsx`)
}

getList()
</script>
